package com.wutong.resonance.model.audio.enums;

import lombok.Getter;

import java.io.File;

/**
 * 音频类型枚举类
 * @author wutong
 */

@Getter
public enum AudioType {
    PCM("pcm"),
    MP3("mp3"),
    WAV("wav"),
    OGG("ogg"),
    FLAC("flac"),
    AAC("aac"),
    AMR("amr"),
    MIDI("midi"),
    APE("ape"),
    WMA("wma"),
    M4A("m4a"),
    AIFF("aiff"),
    AU("au"),
    TTA("tta"),
    WAVPACK("wavpack"),
    DSF("dsf"),
    DFF("dff"),
    DCP("dcp"),
    DTX("dtx"),
    DSF_PACK("dsf_pack"),
    DFF_PACK("dff_pack"),
    DCP_PACK("dcp_pack"),
    DTX_PACK("dtx_pack"),
    UNKNOWN("unknown");

    private final String type;

    AudioType(String type) {
        this.type = type;
    }


    /**
     * 根据类型字符串获取音频类型
     * @param type 类型字符串
     * @return 音频类型
     */
    public static AudioType getAudioType(String type) {
        for (AudioType audioType : AudioType.values()) {
            if (audioType.type.equals(type)) {
                return audioType;
            }
        }
        return UNKNOWN;
    }

    /**
     * 根据文件获取音频类型
     * @param audioFile 音频文件
     * @return 音频类型
     */
    public static AudioType getAudioType(File audioFile) {
        String fileName = audioFile.getName().toLowerCase();
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
        return switch (extension) {
            case "raw", "pcm" -> PCM;
            case "mp3" -> MP3;
            case "wav" -> WAV;
            case "ogg" -> OGG;
            case "flac" -> FLAC;
            case "aac" -> AAC;
            case "amr" -> AMR;
            case "midi" -> MIDI;
            case "ape" -> APE;
            case "wma" -> WMA;
            case "m4a" -> M4A;
            case "aiff" -> AIFF;
            case "au" -> AU;
            case "tta" -> TTA;
            case "wavpack" -> WAVPACK;
            case "dsf" -> DSF;
            case "dff" -> DFF;
            case "dcp" -> DCP;
            case "dtx" -> DTX;
            case "dsf_pack" -> DSF_PACK;
            case "dff_pack" -> DFF_PACK;
            case "dcp_pack" -> DCP_PACK;
            case "dtx_pack" -> DTX_PACK;
            default -> UNKNOWN;
        };
    }
}
